package com.apcc4m.sfpushbatch.writer;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import com.apcc4m.sfpushbatch.repository.Notification;
import com.apcc4m.sfpushbatch.repository.NotificationRepository;
import com.apcc4m.sfpushbatch.repository.PushStatus;
import com.apcc4m.sfpushbatch.repository.PushStatusRepository;
import com.apcc4m.sfpushbatch.repository.User;
import com.apcc4m.sfpushbatch.repository.UserRepository;

public class NotificationItemWriter<T> implements ItemWriter<Notification> {


    @Autowired
    private NotificationRepository notificationRepository;
    @Autowired
    private PushStatusRepository pushStatusRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public void write(List<? extends Notification> items) throws Exception {
        System.out.println("开始将Notification数据迁移至PushStatus中");
        Long t1 = System.currentTimeMillis();
        for (Notification notif : items) {
            List<PushStatus> pushStatusList = new ArrayList<PushStatus>();
            // 1:给所有用户 2:android 3:iOS 4:指定用户
            if (notif.getType() == 1) {
                List<User> userList = userRepository.findAll();
                pushStatusList = doPushStatus(notif, userList);
            } else if (notif.getType() == 2) {
                List<User> userList = userRepository.findByType(1);
                pushStatusList = doPushStatus(notif, userList);
            } else if (notif.getType() == 3) {
                List<User> userList = userRepository.findByType(2);
                pushStatusList = doPushStatus(notif, userList);
            } else if (notif.getType() == 4) {
                Set<String> userIdSet = notif.getUserSet();
                List<User> userList = new ArrayList<User>();
                for (String userId : userIdSet) {
                    User user = userRepository.findOneById(userId);
                    userList.add(user);
                }
                pushStatusList = doPushStatus(notif, userList);
            }
            pushStatusRepository.insert(pushStatusList);
            notif.setStatus(1);
            notificationRepository.save(notif);
        }
        Long t2 = System.currentTimeMillis();
        System.out.println("结束迁移,花费时间(毫秒)" + (t2 - t1));
    }

    public List<PushStatus> doPushStatus(Notification notification, List<User> userList) {
        List<PushStatus> pushStatusList = new ArrayList<PushStatus>();
        for (User user : userList) {
            PushStatus pushStatus = new PushStatus();
            pushStatus.setnId(notification.getId());
            pushStatus.setUserId(user.getId());
            pushStatus.setUserType(user.getType());
            pushStatus.setDeviceId(user.getDeviceId());
            pushStatus.setStatus(0);
            pushStatusList.add(pushStatus);
        }
        return pushStatusList;
    }
}
